package com.zxp.mall.order.web;

import com.zxp.mall.order.entity.OrderEntity;
import com.zxp.mall.order.service.OrderService;
import com.zxp.mall.order.vo.OrderConfirmVo;
import com.zxp.mall.order.vo.OrderSubmitVo;
import com.zxp.mall.order.vo.SubmitOrderResponseVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/confirm.html")
    public String confirm(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo vo = orderService.conformOrder();
        model.addAttribute("orderConfirmData", vo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model){
        SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
//        创建订单 验令牌 验价格 验库存 。。。
//        跳转支付选择页 失败则回到订单确认页重新确认订单信息
        if(responseVo.getCode() == 0) {
            model.addAttribute("submitOrderResp", responseVo);
            return "pay";
        }
        System.out.println(responseVo.getCode());
        return "redirect:http://order.gulimall.com/confirm.html";
    }

    @ResponseBody
    @GetMapping("/test/mq")
    public String createTestMq(){
        OrderEntity order = new OrderEntity();
        order.setOrderSn(UUID.randomUUID().toString());
        order.setModifyTime(new Date());
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",order);
        return "ok";
    }



}
